package cn.wyf.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.wyf.demo.model.Student;

@Controller
@RequestMapping("/home")
public class IndexController {

    private static Logger logger = Logger.getLogger(IndexController.class);
    @RequestMapping("/index")
    public String getIndex() {
        System.out.println("=============");
        logger.error("index.");
        return "index";
    }

    @RequestMapping("/test")
    public @ResponseBody
    String test() {
        System.out.println("======test=======");
        Map<String, Student> map = new HashMap<String, Student>();
        map.put("test", new Student("jack ma", "f", 55));
        map.put("ffff", new Student("jack", "m", 53));
        String jsonString = JSON.toJSONString(map);
        System.out.println(jsonString);
        return jsonString;
    }

    @RequestMapping(value = "/processOrders", method = RequestMethod.POST)
    public @ResponseBody
    String processOrders(@RequestBody JSONObject s) {
        System.out.println("======processOrders=======");
        System.out.println(s);
        String jsonString = "{\"success\":true,\"msg\":\"成功\",\"obj\":{\"Balance\":\"118969\",\"TransTime\":\"2021-04-27 11:11:30\",\"transFinishTime\":\"2021-04-27 11:11:30\",\"serialNo\":\"000016\"},\"number\":null}";
        System.out.println(jsonString);
        return jsonString;
    }

    @RequestMapping(value = "/undoOrders", method = RequestMethod.POST)
    public @ResponseBody
    String undoOrders(@RequestBody JSONObject s) {
        System.out.println("======undoOrders=======");
        System.out.println(s);
        String jsonString = "{\"success\":true,\"msg\":\"成功\",\"obj\":\"2021-04-27 11:30:20\",\"number\":null}";
        System.out.println(jsonString);
        return jsonString;
    }

    @RequestMapping(value = "/getOrderInfo", method = RequestMethod.POST)
    public @ResponseBody
    String getOrderInfo(@RequestBody JSONObject s) {
        System.out.println("======getOrderInfo=======");
        System.out.println(s);
        String jsonString = "{\"success\":true,\"msg\":\"成功\",\"obj\":{\"serialNo\":\"000128\",\"gasStationId\":\"2fd1328d853e4c7b8c4e21427acb1d7a\",\"gasStationNo\":\"600G\",\"terminalId\":\"18965be8f73311eaa5b5005056bc7b77\",\"terminalNo\":\"61002110\",\"cardNo\":\"6100024106783000\",\"copeWith\":1023,\"realPay\":1023,\"balance\":null,\"transTime\":\"2020-09-18 15:45:53\",\"finishTime\":\"2020-09-18 15:45:53\",\"undoTime\":null,\"state\":2,\"transType\":3,\"remark\":null,\"terminalName\":\"测试终端\",\"gasStationName\":\"测试油站\",\"merchantsName\":\"测试商户\",\"merchantsNo\":\"CN6100210000000\",\"orderInfoGoosRes\":[{\"orderId\":\"5d51ba72a1d44271b071d92957a6af16\",\"goodsInfoNo\":\"1022\",\"quantity\":19670,\"price\":5200,\"amount\":1022840,\"goodsInfoName\":\"93号车用汽油（IV）\",\"fid\":\"7371fd1a7fa0460ba3b814831071bb32\"}],\"fid\":\"5d51ba72a1d44271b071d92957a6af16\"},\"number\":null}";
        System.out.println(jsonString);
        return jsonString;
    }

    @RequestMapping(value = "/selectCardBalance", method = RequestMethod.POST)
    public @ResponseBody
    String selectCardBalance(@RequestBody JSONObject s) {
        System.out.println("======selectCardBalance=======");
        System.out.println(s);
        String jsonString = "{\"success\":true,\"msg\":\"成功\",\"obj\":{\"cardNo\":\"6100024106614751\",\"balance\":\"119967\",\"fuelLimit\":\"\"},\"number\":null}";
        System.out.println(jsonString);
        return jsonString;
    }

    @RequestMapping(value = "/testjson2", method = RequestMethod.GET)
    public @ResponseBody
    String jsonTest3() {
        JSONObject object = new JSONObject();
        object.put("name", "jacak ma");
        JSONArray array = new JSONArray();
        for (int i = 0; i < 5; i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title", "da" + i);
            jsonObject.put("age", i + 20);
            array.add(jsonObject);
        }
        object.put("list",array);
        String jsonString =object.toJSONString();
        System.out.println(jsonString);
        return jsonString;
    }


    @RequestMapping(value = "/testjson", method = RequestMethod.GET)
    public @ResponseBody
    Student jsonTest() {
        System.out.println("==========testjson============");
        return new Student("jack", "f", 18);
    }

    //本方法将处理 /home/testjson1/123 形式的URL
    @RequestMapping(value = "/testjson1/{id}", method = RequestMethod.GET)
    public ResponseEntity<Student> jsontest2(@PathVariable Integer id) {
        System.out.println("=============2=========" + id);
        Student student = new Student("Tom" + id, "m", 20);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }

    //本方法将处理 /home/view?id=123 形式的URL
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ResponseEntity<Student> viewStudent(@RequestParam("id") Integer id) {
        System.out.println("In viewStudent, id = {}" + id);
        Student student = new Student("Jerry" + id, "f", 24);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }

    //本方法将处理 /home/view1?id=123 形式的URL
    @RequestMapping("/view1")
    public ResponseEntity<Student> viewStudent1(HttpServletRequest request) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        System.out.println("In viewStudent1, id = {}" + id);
        Student student = new Student("Money" + id, "f", 22);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }

    @RequestMapping(value = "/test3",method = RequestMethod.POST)
    @ResponseBody
    public String test(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        JSONObject object = new JSONObject();
        object.put("username",username);
        object.put("password",password);
        System.out.println(object.toJSONString());
        return object.toJSONString();
    }
}